<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
<!--      侧边栏区域-->
      <el-aside :width="isCollapsed?'64px':'200px'">
<!--        折叠区域-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#313743"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapsed"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
<!--          一级菜单-->
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
<!--            一级菜单的模板区域-->
            <template slot="title">
<!--              图标-->
              <i :class="iconsObj[item.id]"></i>
<!--              文本内容-->
              <span>{{item.authName}}</span>
            </template>
<!--二级菜单-->
            <el-menu-item :index="'/'+subitem.path" v-for="subitem in item.children" :key="subitem.id" @click="saveNavStatus('/'+subitem.path)">
              <template slot="title">
                <!--              图标-->
                <i class="el-icon-menu"></i>
                <!--              文本内容-->
                <span>{{subitem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
<!--      主体区域-->
      <el-main>
<!--        路由占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  export default {
    data(){
      return {
        //左侧菜单数据
        menuList:[],
        iconsObj:{
          '125':"el-icon-user-solid",
          '103':"el-icon-film",
          '101':"el-icon-s-goods",
          '102':"el-icon-tickets",
          '145':"el-icon-s-marketing"
        },
        //默认不折叠
        isCollapsed:false,
        //被激活的链接的地址
        activePath:''
      }
    },
    created (){
      this.getMenuList()
      this.activePath=window.sessionStorage.getItem('activePath')
    },
    methods:{
      //退出登录
      logout (){
        window.sessionStorage.clear();
        this.$router.push("/login")
      },
      //获取菜单的数据
      async getMenuList (){
        const {data:res} = await this.$http.get('menus')
        console.log(res)
        if(res.meta.status!==200) return this.$message.error(res.meta.msg)
        this.menuList=res.data
      },
      //点击按钮，折叠或者显示菜单
      toggleCollapse (){
        this.isCollapsed=!this.isCollapsed
      },
      //获取链接的激活状态
      saveNavStatus (activePath){
        window.sessionStorage.setItem('activePath',activePath)
        this.activePath=activePath
      }
    }
  }
</script>

<style lang="less" scoped>
  .home-container{
    height: 100%;
  }
.el-header{
  background-color:#353c3f;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  >div{
    display: flex;
    align-items: center;
    span{
      margin-left: 15px;
    }
  }
}
  .el-aside{
    background-color: #313743;
    .el-menu{
      border-right: none;
    }
  }
  .el-main{
    background-color: #e9edf1;
  }
  .toggle-button{
    background-color: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
